Ext.define('Hr.controller.depart.DepartInfoController',{
    extend:'Ext.app.Controller',
    stores:['depart.DepartInfoStore','depart.DepartInfoComboStore'],
    models:[],
    views:['depart.ShowPanel','depart.EditWindow'],
    init:function(){
        var id;
        var data;
        var flag;
        this.control({
            //show面板控制器
            'show grid':{
                render:function(){
                    var store = Ext.getCmp('grid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':null,"val":null});
                    });
                    store.load();
                },
                selectionchange:function(model, records,obj){
                    data = records[0];
                    try{id = records[0].raw.id;}catch(e){}
                    Ext.getCmp('del').setDisabled(false);
                    Ext.getCmp('update').setDisabled(false);
                }
            },
            'show button[action = add]':{
                click:function(){
                    Ext.create('Hr.view.depart.EditWindow').show();
                    flag = true;
                }
            },
            'show button[action = update]':{
                click:function(){
                    Ext.create('Hr.view.depart.EditWindow').show();
                    var form = Ext.getCmp('form').getForm();
                    form.loadRecord(data);
                    flag = false;
                }
            },
            'show button[action = del]':{
                click:function(){
                    Ext.MessageBox.show({
                        title:'警告！',
                        msg:'<center>删除后数据无法恢复，<br/>确定删除？</center>',
                        buttons:Ext.Msg.YESNO,
                        icon:Ext.Msg.WARNING,
                        fn:function(is){
                            if (is=='yes') {
                                Ext.Ajax.request({
                                    url:'../departInfoAction/del.do',
                                    params:{'id':id},
                                    success:function(){
                                        Ext.MessageBox.alert("提示","删除成功！");
                                        var store = Ext.getCmp('grid').store;
                                        store.remove(data);
                                        Ext.getCmp('del').setDisabled(true);
                                        Ext.getCmp('update').setDisabled(true);
                                    }
                                })
                            }
                        }
                    })
                }
            },
            'show button[action = search]':{
                click:function(){
                    var name = Ext.getCmp('textfield').getValue();
                    var val = Ext.getCmp('combo').getValue();
                    var store = Ext.getCmp('grid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':name,"val":val});
                    });
                    store.load();
                }
            },
            //edit面板控制器
            'edit button[action = submit]':{
                 click:function(obj){
                    var form = obj.up('form').getForm();
                    var window  = obj.up('edit');
                    if(form.isValid()){
                        Ext.Ajax.request({
                            url:'../departInfoAction/addOrUpdate.do',
                            params:form.getValues(true),
                            success:function(){
                                var store = Ext.getCmp('grid').store;
                                if(flag){
                                    store.add(form.getValues());
                                    Ext.MessageBox.show({
                                        title:'提示',
                                        msg:'添加成功！<br>继续添加？',
                                        buttons:Ext.Msg.YESNO,
                                        fn:function(is){
                                            if(is=='no'){
                                                window.close();
                                            }else{
                                                form.reset();
                                            }
                                        }
                                    })
                                }else{
                                    Ext.MessageBox.alert("提示",'修改成功！');
                                    window.close();
                                    store.load();
                                    Ext.getCmp('del').setDisabled(false);
                                    Ext.getCmp('update').setDisabled(false);
                                }
                            },
                            failure:function(){
                                Ext.MessageBox.show({
                                    title:'错误',
                                    msg:'<center>服务器传来一项错误！<br/>可能是因为“部门编号”重复！</center>',
                                    buttons:Ext.Msg.OK,
                                    icon:Ext.Msg.ERROR
                                });
                            }
                        })
                    }
                 }
            }
        });
    }
})